﻿@using Damselfly.Core.ScopedServices.ClientServices
@inject ViewDataService ViewDataService
@inject IUserStatusService statusService
@inject RestClient httpClient

<div class="damselfly-logview">
    @if( logLines == null || !logLines.Any() )
    {
        <p>Loading log entries....</p>
    }
    else
    {
        <div class="damselfly-searchbar">
            <div class="damselfly-browsetools">
                <h4 @onclick="DownloadLogFile">@LogFileName</h4>
                &nbsp;
                &nbsp;
                &nbsp;
                <select class="damselfly-browsetool damselfly-browsetool-select" @bind="SelectedFilter">
                    <option value="ALL">Show All Logs</option>
                    <option value="INF">Show Info</option>
                    <option value="WRN">Show Warnings</option>
                    <option value="ERR">Show Errors</option>
                </select>
            </div>
            <div class="log-scroll-area">
                <div class="log-table">
                    <table class="loglist-table">
                        <thead>
                        <tr>
                            <th class="timestamp">Timestamp</th>
                            <th class="level">Log Level</th>
                            <th class="entry">Entry</th>
                        </tr>
                        </thead>
                        <tbody>
                        <Virtualize Items="@filteredLogs" Context="line" ItemSize="15">
                            <tr>
                                <td class="timestamp">@line.Date</td>
                                <td class="level log-level-@line.Level">@line.Level</td>
                                <td class="entry">@line.Entry</td>
                            </tr>
                        </Virtualize>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    }
</div>

@code {


    List<LogEntry> logLines = new();
    private string LogFileName { get; set; }
    private string SelectedFilter { get; set; } = "ALL";

    private List<LogEntry> filteredLogs => logLines.Where(x => SelectedFilter == "ALL" || x.Level == SelectedFilter).ToList();

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if( firstRender )
        {
            for( var page = 0; page < 20; page++ )
            {
                var response = await httpClient.CustomGetFromJsonAsync<LogEntryResponse>($"/api/logs/{page}");

                LogFileName = response.LogFileName;

                if( response.LogEntries != null )
                {
                    logLines.AddRange(response.LogEntries);
                    StateHasChanged();
                    statusService.UpdateStatus($"Loaded {logLines.Count()} log lines...");
                }
            }
        }
    }

    private async Task DownloadLogFile()
    {
        // TODO: Download log file here
        await Task.Delay(500);
    }

}